# Maintainer: Rodrigo Hernandez <kwizatz@aeongames.com>
# Contributor: Raed Rizqie <raed.rizqie@gmail.com>

_realname=v8
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=11.8.7
pkgrel=1
pkgdesc="Fast and modern Javascript engine (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32')
url="https://v8.dev"
license=('spdx:BSD-3-Clause')
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-gn"
             "${MINGW_PACKAGE_PREFIX}-ninja"
             "${MINGW_PACKAGE_PREFIX}-pkgconf"
             "${MINGW_PACKAGE_PREFIX}-python"
             "git")
depends=("${MINGW_PACKAGE_PREFIX}-icu"
         "${MINGW_PACKAGE_PREFIX}-zlib")
source=("https://github.com/v8/v8/archive/refs/tags/${pkgver}.tar.gz"
        "bare-clones/build::git+https://chromium.googlesource.com/chromium/src/build.git#commit=a5321f10c92e1cab7ba5ba6f8a4731513db090c5"
        "bare-clones/abseil-cpp::git+https://chromium.googlesource.com/chromium/src/third_party/abseil-cpp.git#commit=583dc6d1b3a0dd44579718699e37cad2f0c41a26"
        "bare-clones/zlib::git+https://chromium.googlesource.com/chromium/src/third_party/zlib.git#commit=526382e41c9c5275dc329db4328b54e4f344a204"
        "bare-clones/common::git+https://chromium.googlesource.com/chromium/src/base/trace_event/common.git#commit=147f65333c38ddd1ebf554e89965c243c8ce50b3"
        "bare-clones/googletest::git+https://chromium.googlesource.com/external/github.com/google/googletest.git#commit=af29db7ec28d6df1c7f0f745186884091e602e07"
        "bare-clones/jinja2::git+https://chromium.googlesource.com/chromium/src/third_party/jinja2.git#commit=515dd10de9bf63040045902a4a310d2ba25213a0"
        "bare-clones/markupsafe::git+https://chromium.googlesource.com/chromium/src/third_party/markupsafe.git#commit=006709ba3ed87660a17bd4548c45663628f5ed85"
        "bare-clones/clang::git+https://chromium.googlesource.com/chromium/src/tools/clang.git#commit=078c92947ef8554ac4da1bb351c284bf2230b94f"
        "001-add-mingw-toolchain.patch"
        "002-buildflags-fixes.patch"
        "003-fix-macros-and-functions.patch"
        "004-fix-static-assert-implementations.patch"
        "005-fix-conflicting-macros.patch"
        "006-support-clang-in-mingw-mode.patch"
        "007-snapshot-use-system-zlib-header.patch"
        "008-prioritized-native-thread-on-windows.patch"
        "009-unicode-for-wide-char-functions.patch"
        "010-disable-msvc-hack.patch"
        "011-make-sure-that-__rdtsc-is-declared.patch"
        "012-remove-dllimport-attributes.patch"
        "013-builtin-deps-fixes.patch"
        "014-fix-missing-include.patch"
        "015-abseil-build-as-static-lib.patch"
        "016-zlib-use-sytem-lib.patch"
        "icu.gn"
        "zlib.gn"
        "v8_libbase.pc"
        "v8_libplatform.pc"
        "v8.pc")
sha256sums=('7c96843bd65ae290bf7424be546b244b67d2f4fb5ae1b5922942a36dae855f62'
            'SKIP'
            'SKIP'
            'SKIP'
            'SKIP'
            'SKIP'
            'SKIP'
            'SKIP'
            'SKIP'
            '65e27b704a9b45f9e879ee5e7281d0d1e60ac819f6088f0b07c211164a0e9524'
            '28092f71029393b52aa4315a9d38b59faee221220d5f380a16438e7a2d650337'
            '06b5ac9c0389291aff7d0c8fe096af50713385f3be9df0326535208e49e1a0f4'
            'bd1976ddb9ca8387c125ee578c9b66d85e27f989544e53e4ca0598bbd7ce52e9'
            '75a0d6fbf04cb9bbb724230104cf28595cd9b1169a0e91a4090bd0d3b6f80a8c'
            'cbd3bbdafe4e8f315086328054c6c6a0f053509b6cd0e0a01f8aaecc42729de3'
            '1fca982ec6ede3b29d2692231569944ce0ad33519b47fd6fb01b0975c10eb6bd'
            '3855f5d7b81c2df37f31de09ae9638d7247b78c5798ac18a58ab6e6be3befe5c'
            'e46320e6b09c9d7d822ee6ff37b4ceb828de98b57f2b8eb988ab3e5e6ad493bb'
            'e4d8e57914cfcf6c0ea299d0556d7b747271e5a7ec66e1d8d52394fcd0da34b6'
            'c7d119eb59aa34898efda6fdaf5b2eabeb3390d527531ad54f210fc1e733c75a'
            '3cb604aefbe6be06598b8327f8ed0f5bff688f39978b70f7fee4a1fb107ad199'
            '37a29668fb02d4fa60062c02f0d24dce4ab9b7fce4d7f5a8e6eca745fa125be2'
            '0b142b795a4db7d063825d9578503f275632eb0139b5ab4a7fc75e45201ab04a'
            'fe9d935e648ec4fbd40b033528ac97d3b8c374890ea42ad4a5ca3df335ff57e8'
            'a4275f41723cc990f2f43559ce716100830a8ce3f38e4878f6bf36d70d47b646'
            'e1bc88af48143f29f5805b8ac5d77addef1b09318d1f5f127a9281872c00303b'
            '7e1e39a3dc69632f7bf9182c92405186ce709aca921cf0abefadb6fa8b825510'
            'fbddbe4849b623afb320472aab316c9a08b54182161b6b4911cb7ef7d65e2b0e'
            'ca81528892f0e02fe7ecd159568610221fb8d0df109d9d0dbdfa00c1284dd0fc'
            '579e199a798ff76b72a5b17380a3cd09d361cb7cee99403090c3a266d1b2e527')

prepare() {
  echo ":: Patching build directory"
  cd ${srcdir}/build
  patch -p1 -i ${srcdir}/001-add-mingw-toolchain.patch
  python ${srcdir}/build/util/lastchange.py -o ${srcdir}/build/util/LASTCHANGE
  rm -rf ${srcdir}/${_realname}-${pkgver}/build
  ln -sf ${srcdir}/build ${srcdir}/${_realname}-${pkgver}
  echo "checkout_google_benchmark = false" > ${srcdir}/${_realname}-${pkgver}/build/config/gclient_args.gni

  echo ":: Patching abseil-cpp directory"
  cd ${srcdir}/abseil-cpp
  patch -p1 -i ${srcdir}/015-abseil-build-as-static-lib.patch
  ln -sf ${srcdir}/abseil-cpp ${srcdir}/${_realname}-${pkgver}/third_party

  echo ":: Patching zlib directory"
  cd ${srcdir}/zlib
  patch -p1 -i ${srcdir}/016-zlib-use-sytem-lib.patch
  mkdir -p ${srcdir}/${_realname}-${pkgver}/third_party/zlib
  ln -sf ${srcdir}/zlib.gn ${srcdir}/${_realname}-${pkgver}/third_party/zlib/BUILD.gn
  rm -rf ${srcdir}/${_realname}-${pkgver}/third_party/zlib/google
  ln -sf ${srcdir}/zlib/google ${srcdir}/${_realname}-${pkgver}/third_party/zlib

  echo ":: Patching icu directory"
  mkdir -p ${srcdir}/${_realname}-${pkgver}/third_party/icu
  ln -sf ${srcdir}/icu.gn ${srcdir}/${_realname}-${pkgver}/third_party/icu/BUILD.gn

  echo ":: Soft linking extra repos"
  mkdir -p ${srcdir}/${_realname}-${pkgver}/base/trace_event
  ln -sf ${srcdir}/common ${srcdir}/${_realname}-${pkgver}/base/trace_event
  mkdir -p ${srcdir}/${_realname}-${pkgver}/third_party/googletest/src
  ln -sf ${srcdir}/googletest/* ${srcdir}/${_realname}-${pkgver}/third_party/googletest/src
  ln -sf ${srcdir}/jinja2 ${srcdir}/${_realname}-${pkgver}/third_party
  ln -sf ${srcdir}/markupsafe ${srcdir}/${_realname}-${pkgver}/third_party
  ln -sf ${srcdir}/clang ${srcdir}/${_realname}-${pkgver}/tools

  echo ":: Patching v8 source"
  cd ${srcdir}/${_realname}-${pkgver}
  patch -p1 -i ${srcdir}/002-buildflags-fixes.patch
  patch -p1 -i ${srcdir}/003-fix-macros-and-functions.patch
  patch -p1 -i ${srcdir}/004-fix-static-assert-implementations.patch
  patch -p1 -i ${srcdir}/005-fix-conflicting-macros.patch
  patch -p1 -i ${srcdir}/006-support-clang-in-mingw-mode.patch
  patch -p1 -i ${srcdir}/007-snapshot-use-system-zlib-header.patch
  patch -p1 -i ${srcdir}/008-prioritized-native-thread-on-windows.patch
  patch -p1 -i ${srcdir}/009-unicode-for-wide-char-functions.patch
  patch -p1 -i ${srcdir}/010-disable-msvc-hack.patch
  patch -p1 -i ${srcdir}/011-make-sure-that-__rdtsc-is-declared.patch
  patch -p1 -i ${srcdir}/012-remove-dllimport-attributes.patch
  patch -p1 -i ${srcdir}/013-builtin-deps-fixes.patch
  patch -p1 -i ${srcdir}/014-fix-missing-include.patch

  sed -e "s|@VERSION@|${pkgver}|g" \
      -e "s|@CFLAGS@|${_cflags}|g" \
      -e "s|^prefix=.*|prefix=${MINGW_PREFIX}|g" -i "${srcdir}/v8_libbase.pc"

  sed -e "s|@VERSION@|${pkgver}|g" \
      -e "s|@CFLAGS@|${_cflags}|g" \
      -e "s|^prefix=.*|prefix=${MINGW_PREFIX}|g" -i "${srcdir}/v8_libplatform.pc"

  sed -e "s|@VERSION@|${pkgver}|g" \
      -e "s|@CFLAGS@|${_cflags}|g" \
      -e "s|^prefix=.*|prefix=${MINGW_PREFIX}|g" -i "${srcdir}/v8.pc"
}

build() {
  cd ${_realname}-${pkgver}

  local _buildtype=Release
  local _debug=false
  if check_option "debug" "y"; then
    _buildtype=Debug
    _debug=true
  fi

  local _arch=x64
  if [[ ${CARCH} == i686 ]]; then
    _arch=x86
  fi

  local _clang=false
  if [[ ${MSYSTEM} == CLANG* ]]; then
    _clang=true
  fi

  # generate build files
  ${MINGW_PREFIX}/bin/gn gen out/${_buildtype}-${MSYSTEM} --args="
    target_cpu=\"${_arch}\"
    is_debug=${_debug}
    is_clang=${_clang}
    is_component_build=true
    use_custom_libcxx=false
    use_lld=false
    use_sysroot=false
    v8_enable_verify_heap=false
    icu_use_data_file=false
    enable_iterator_debugging=${_debug}
    treat_warnings_as_errors=false"

  ${MINGW_PREFIX}/bin/ninja -C out/${_buildtype}-${MSYSTEM} v8_libbase v8_libplatform v8 d8 torque torque-language-server mksnapshot
}

package() {
  cd ${_realname}-${pkgver}

  local _buildtype=Release
  if check_option "debug" "y"; then
    _buildtype=Debug
  fi

  install -d ${pkgdir}${MINGW_PREFIX}/{bin,include/{cppgc/internal,libplatform},lib/pkgconfig,share/licenses/${_realname}}
  install -Dm755 out/${_buildtype}-${MSYSTEM}/d8.exe                      ${pkgdir}${MINGW_PREFIX}/bin/d8.exe
  install -Dm755 out/${_buildtype}-${MSYSTEM}/torque.exe                  ${pkgdir}${MINGW_PREFIX}/bin/torque.exe
  install -Dm755 out/${_buildtype}-${MSYSTEM}/torque-language-server.exe  ${pkgdir}${MINGW_PREFIX}/bin/torque-language-server.exe
  install -Dm755 out/${_buildtype}-${MSYSTEM}/mksnapshot.exe              ${pkgdir}${MINGW_PREFIX}/bin/mksnapshot.exe
  install -Dm755 out/${_buildtype}-${MSYSTEM}/*.dll                       ${pkgdir}${MINGW_PREFIX}/bin/
  install -Dm644 out/${_buildtype}-${MSYSTEM}/snapshot_blob.bin           ${pkgdir}${MINGW_PREFIX}/bin/snapshot_blob.bin
  install -Dm755 out/${_buildtype}-${MSYSTEM}/*.a                         ${pkgdir}${MINGW_PREFIX}/lib/
  install -Dm644 $srcdir/*.pc                                             ${pkgdir}${MINGW_PREFIX}/lib/pkgconfig/
  install -Dm644 include/*.h                                              ${pkgdir}${MINGW_PREFIX}/include/
  install -Dm644 include/cppgc/*.h                                        ${pkgdir}${MINGW_PREFIX}/include/cppgc/
  install -Dm644 include/cppgc/internal/*.h                               ${pkgdir}${MINGW_PREFIX}/include/cppgc/internal/
  install -Dm644 include/libplatform/*.h                                  ${pkgdir}${MINGW_PREFIX}/include/libplatform/
  install -Dm644 LICENSE*                                                 ${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/
}
